<?php
/**
 * This file is part of the Sphoof framework.
 * Copyright (c) 2010-2011 Sphoof
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code. You can also view the
 * LICENSE file online at http://www.sphoof.nl/new-bsd.txt
 *
 * @category	Sphoof
 * @copyright	Copyright (c) 2010-2011 Sphoof (http://sphoof.nl)
 * @license		http://sphoof.nl/new-bsd.txt	New BSD License
 * @package		Messenger
 */

/**
 * Messenger is a class that uses a variable in the SpSession object to save
 * messages, so they can be retrieved on a later pageload. The standard usage
 * is to place during a HTTP-POST
 *
 * $messenger->add( 'error', 'Your action could not be performed' );
 * $messenger->add( 'info', 'Your last login was 2010-04-02' );
 * $messenger->add( 'success', 'The email has been sent' );
 *
 * Then, in a template, you could loop through all types of messages available.
 * In this case it would be 'error', 'info'  and 'success'
 *
 * foreach ( $messenger->types() as $type ) {
 *   echo '<ul class="'.$type.'">';
 *   foreach ( $messenger->get( $type ) as $message ) {
 *     echo '<li>' . htmlspecialchars($message) . '</li>';
 *   }
 *   echo '</ul>';
 * }
 *
 * After the messages have been retrieved, they are deleted from the memory.
 *
 */
class SpMessenger {

	/**
	 * The wrapper of the $_SESSION variable
	 *
	 * @var SpSession
	 */
    protected $session;

	/**
	 * The namespace in which all data is stored
	 *
	 * @var string
	 */
	protected $namespace = 'SpMessenger';

	/**
	 * @param SpSession $session
	 */
	public function  __construct( SpSession $session ) {
		$this->session = $session;
	}

	/**
	 * Retrieve all messages of a single type, such as 'error',
	 * 'info' or 'success'. Then remove the messages from the
	 * storage.
	 *
	 * @param string $type
	 * @return array
	 */
	public function get( $type ) {
		$info = $this->info();
		if ( isset($info[$type]) ) {
			$this->clear($type);
			return array_reverse( $info[$type], true );
		}
		return array();
	}

	/**
	 * Add a message of a certain type, for example 'error',
	 * 'info' or 'success'.
	 *
	 * @param string $type
	 * @param string $message
	 */
	public function add( $type, $message ) {
		$info = $this->info();
		if ( !isset($info[$type]) ) {
			$info[$type] = array();
		}
		$info[$type][] = $message;
		$this->session->set($this->namespace, $info);
	}

	/**
	 * Retrieve all available message types. Very useful for looping
	 * through all available messages.
	 *
	 * @return array
	 */
	public function types() {
		return array_keys($this->info());
	}

	/**
	 * Get all stored messages from the storage
	 *
	 * @return array
	 */
	protected function info( ) {
		return $this->session->get($this->namespace,array());
	}

	/**
	 * Remove one type of messages from the storage
	 *
	 * @param string $type
	 * @return SpMessenger
	 */
	protected function clear($type) {
		$info = $this->info();
		unset($info[strtolower($type)]);
		$this->session->set($this->namespace, $info);
		return $this;
	}

}